<template>
  <div class="home">
    <el-container>
      <el-header class="homeHeader">
        <div class="title">ATM银行管理系统</div>
        <div>
          <el-button icon="el-icon-bell"
                     type="text"
                     size="normal"
                     style="margin-right: 8px; color: black"
                     @click="goChat">
          </el-button>
          <el-dropdown class="userInfo" @command="commandHandler">
                     <span class="el-dropdown-link">
                       {{ user[0].username }}<i><img :src="user[0].image"></i>
                     </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
              <el-dropdown-item command="setting">找回密码</el-dropdown-item>
              <el-dropdown-item command="logout">注销登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
              style="text-align: left;overflow: hidden"
              default-active="1"
              router
              class="el-menu-vertical-demo"
              active-text-color="#409EFF"
              @open="handleOpen"
              @close="handleClose">
            <el-submenu index="1">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>信用卡</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="account">我的账户</el-menu-item>
                <el-menu-item index="AddCard">办理卡</el-menu-item>
                <el-menu-item index="addmoney">存款</el-menu-item>
              </el-menu-item-group>
            </el-submenu>

            <el-submenu index="3">
              <template slot="title">
                <i class="el-icon-document"></i>
                <span slot="title">转账支付</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="Transfer">转账</el-menu-item>
                <el-menu-item index="TransLog">交易记录</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="5">
              <template slot="title">
                <i class="el-icon-setting"></i>
                <span slot="title">后台管理</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="user">用户</el-menu-item>
                <el-menu-item index="trans">流水</el-menu-item>
                <el-menu-item index="card">开户卡</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-container>
          <el-main>
            <el-breadcrumb separator-class="el-icon-arrow-right"
                           v-if="this.$router.currentRoute.path!=='/home'">
              <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ this.$router.currentRoute.name }}</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="homeWelcome" v-if="this.$router.currentRoute.path==='/home'">
              欢迎来到银行管理系统！
            </div>
            <router-view class="homeRouterView"/>
          </el-main>
        </el-container>
      </el-container>
    </el-container>
    <el-dialog title="修改用户密码" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="身份证号" :label-width="formLabelWidth" required>
          <el-input v-model="form.Card" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" :label-width="formLabelWidth" required>
          <el-input v-model="form.newpass" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Home",
  data() {
    return {
      user: JSON.parse(window.localStorage.getItem("user")),
      form: {
        Card: "",
        newpass: "",
      },
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  mounted() {
  },
  methods: {
    goChat() {
      // this.$router.push('/chat');
    },
    commandHandler(command) {
      if (command === 'logout') {
        this.$confirm('此操作将注销登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //清空用户信息
          window.sessionStorage.removeItem('token');
          window.localStorage.removeItem('user');
          //跳转到登录页
          this.$router.replace('/')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          });
        });
      }
      if (command === 'userinfo') {
        // this.$router.push('/userinfo');
        window.open("http://202.182.125.24:44789/")
      }
      if (command === "setting") {
        this.dialogFormVisible = true
      }

    },
    submit() {
      if (this.user[0].pid === this.form.Card) {
        const data = {
          customerID: this.user[0].customerID,
          password: this.form.newpass
        }
        this.putRequest("/api/user", data).then(resp => {
          if (resp) {
            if (resp.code == 1) {
              this.$message.success("修改成功")
              this.dialogFormVisible = false;

            }
          }
        })
      } else {
        this.$message.error("原密码错误")
      }
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }

  }
}
</script>
<style scoped>


.homeHeader {
  background: #409eff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}

.homeHeader .title {
  font-size: 30px;
  font-family: 新宋体;
  color: white;
}

.homeHeader .userInfo {
  cursor: pointer;
}

.el-dropdown-link img {
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 8px;
}

.homeWelcome {
  text-align: center;
  font-size: 30px;
  font-family: 新宋体;
  color: #409eff;
  padding-top: 50px;
}

.homeRouterView {
  margin-top: 10px;
}
</style>
